package p.ithorns.integration.zk.common;

import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

/**
 * ZkUtils
 * TODO
 *
 * @author ithorns
 * @version 1.0
 * @date 2019/9/21 22:01
 */
public class ZkUtils {


    private static String address = "192.168.2.111:2181,192.168.2.112:2181,192.168.2.113:2181,192.168.2.114:2181/test";

    private static ZooKeeper zk;

    private static CountDownLatch cdl = new CountDownLatch(1);

    private static DefaultWatcher defaultWatcher = new DefaultWatcher();

    // 返回对象
    public static ZooKeeper getZk() {
        try {
            defaultWatcher.setCdl(cdl);
            zk = new ZooKeeper(address, 3000, defaultWatcher);
            cdl.await();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
        return zk;
    }

}
